package net.fpeg.kotlin.msa.wordbase.controller

import net.fpeg.kotlin.msa.wordbase.dto.WordNoteDto
import net.fpeg.kotlin.msa.wordbase.service.WordNoteService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("wordNote")
class WordNoteController {

    @Autowired
    lateinit var wordNoteService: WordNoteService

    @GetMapping("/{wordValue}")
    fun fetch(
            @RequestHeader("USER_ID") uid: Long,
            @PathVariable("wordValue") wordValue: String
    ) = wordNoteService.getOne(uid, wordValue).run {
        WordNoteDto(id ?: throw Exception("未知错误"), value)
    }


    @PutMapping("/{wordValue}")
    fun edit(
            @RequestHeader("USER_ID") uid: Long,
            @PathVariable("wordValue") wordValue: String,
            @RequestBody wordNoteDto: WordNoteDto) =
            wordNoteService.edit(uid, wordValue, wordNoteDto).id

}